Hey there.
In this lecture, we're now going to be scripting the UI for our guns to display how much ammo is in
our magazines, as well as how much reserve ammo we have left in our gun.
So if I shoot my gun, as you can see, the magazine ammo goes down and then when I reload, it'll display
a message of reloading and then it subtracts that from our reserve ammo.
So let's get started.
Inside of the local script we've been editing for our guns, we can go ahead and grab the guy that's
inside of each one of our guns, which is called our gun guy.
We'll make a variable called Guy, and that's going to be equal to our gun dot gun guy.
And now what we can go ahead and do is when we scroll down and we equip our gun, we want to go ahead
and display it on our screen so we can change the parent of our gun guy to be equal to our players player
guy folder, and that will allow it to render onto the screen.
And then when we unequip our guns, we want to go ahead and put the guy back inside of the gun.
So right here we can go ahead and refer to guy and set the parent back equal to be inside of our tool.
Now in order to display when the ammo in our gun changes, we can go ahead and listen to the attribute
changed event on our guns, which will notify us when either the ammo and our magazine or the ammo reserve
attribute has been updated by the server, and then we can go ahead and display that new value on our
guy.
So we'll refer to our gun, get the attribute changed event, and then we'll connect a function.
And this will get passed the name of the attribute that got changed.
So we'll just call it name.
Then we can go ahead and check to see if the name is equal to our ammo and mag attribute.
If it is, then inside of our GUI and inside of the frame, we have the mag ammo text, and we can go
ahead and update the text inside of there to be equal to something like mag ammo, and then we can go
ahead and concatenate this with the gun's current ammo that's in the magazine.
So we'll do get attribute ammo in mag.
Otherwise we can check if this attribute that changed is the ammo reserve attribute, and if it is,
we can do the exact same thing that we did here.
But this time we want to change the reserve ammo text.
And here we can say something like reserve ammo and then we'll get the attribute.
Ammo reserve.
So now our guy is going to update when either of these two attributes inside of our gun changes, which
is very cool.
Now, something to note is that by default, if I go and move one of my guys into my starter GUI folder
and then let me enable UI visibility.
As you can see, by default the text is blank.
So when this local script inside of our gun first executes, what we want to do is down in this main
section, we want to go ahead and update the text inside of our gun, so we can just copy this and paste
that down there, and then copy this and paste that down there as well.
Now, the next thing we want to do is we want to go ahead and listen for when our gun is reloading,
and then update the text inside of our gun to say, reloading, and then revert it back to the original
text so we can go to our reload function.
And inside of here, when we're reloading, what we can go ahead and do is.
You can go ahead and refer to our guy, get the frame, and then get the mag ammo text and update the
text inside of it to be equal to something like reloading.
And when the gun is finished reloading, that attribute changed event will automatically fire because
the server will update the attributes inside of our gun, it'll reset the text.
However, we also need to account for the fact that we may not fire to the server to validate our reload
if we unequipped our gun, so we can put an else statement here to check to see, just in case our player
unequipped our gun during the reload animation, and we want to go ahead and update the text back to
the default of mag ammo.
And then we concatenate that with gun get attribute ammo in mag.
And I believe that's all we need to do to implement our GUI.
So we can go ahead and copy our gun from the tools folder, place it inside of our starter pack, and
let's go ahead and see if it's working as we'd expect.
This is why I go and play test.
My game here and I were to equip my gun.
There we go.
It tells us the current ammo in our magazine as well as our reserve ammo.
And then if I were to shoot my gun, as you can see the attribute on our gun updated so it also updated
the text.
And then if I hit R and reload, as you can see it says reloading.
And then both the magazine ammo and the reserve ammo has been updated.
Very cool.
Now, if I shoot and then I reload and I unequip in the middle of my reloading animation, then it should
reset the text to go back to the default.
So if I equip it again, as you can see, we're back to mag ammo and it still says we only have 18 bullets,
but if I reload again.
Now it is updated back to 20 bullets and it removed some from our reserve ammo.
Very cool.
Okay, so we're almost done with our guns.
The last thing we're going to need to do is add hit markers.
When we hit a character model, and our guns will be completed.
I'll see you in the next lecture.